package com.system.controller;

import com.github.pagehelper.PageInfo;
import com.system.po.Userlogin;
import com.system.service.UserloginService;
import com.system.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import springfox.documentation.schema.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;


import javax.servlet.http.HttpServletRequest;

@Controller
@Api(description = "登录接口")
public class LoginController {

    @Autowired
    private UserloginService userloginService;
    //登录跳转
    @RequestMapping(value = "/login", method = {RequestMethod.GET})
    public String loginUI() throws Exception {
        return "../../login";
    }

    //登录表单处理
    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    @ApiOperation(value = "登录页面")
    public String login(Userlogin userlogin, HttpServletRequest request) throws Exception {
        //Shiro实现登录
        UsernamePasswordToken token = new UsernamePasswordToken(userlogin.getUsername(), userlogin.getPassword());
        Subject subject = SecurityUtils.getSubject();

        //如果获取不到用户名就是登录失败，但登录失败的话，会直接抛出异常
        subject.login(token);
        subject.getPrincipal();


        if (subject.hasRole("admin")) {
            return "admin/contingencyPlan";
        } else if (subject.hasRole("teacher")) {
            return "redirect:/teacher/showCourse";
        } else if (subject.hasRole("student")) {
            return "redirect:/student/showCourse";
        }

        return "/login";
    }

    @RequestMapping(value = "/user/addUser", method = {RequestMethod.POST})
    @ApiOperation(value = "添加用户")
    @ResponseBody
    public Result addUser(Userlogin user){
        boolean u= userloginService.addUser(user);
        return u?Result.ok("添加成功"):Result.fail("添加失败");
    }

    @RequestMapping(value = "/user/list", method = {RequestMethod.POST})
    @ApiOperation(value = "用户列表")
    @ResponseBody
    public Result findBypage(Integer page,Integer limit){
        PageInfo<Userlogin> u= userloginService.findBypage(page,limit);
        return u.getSize()>0?Result.ok(u):Result.fail("查询失败或数据为空");
    }


    @RequestMapping(value = "/user/{id}", method = {RequestMethod.DELETE})
    @ApiOperation(value = "删除用户")
    @ResponseBody
    public Result deleteUserById(@PathVariable ("id") Integer id){
        boolean u= userloginService.deleteUser(id);
        return u?Result.ok("删除成功"):Result.fail("删除失败");
    }
}
